package com.meilai;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

@Slf4j
@SpringBootApplication
@EnableScheduling
public class IndexApplication {

    public static void main(String[] args) {
        Environment env = SpringApplication.run(IndexApplication.class, args).getEnvironment();
        String port = env.getProperty("server.port", "8080");
        String contextPath = cleanContextPath(env.getProperty("server.servlet.context-path", ""));
        log.info("Access URLs:\n----------------------------------------------------------\n\t"
                        + "swagger: \thttp://localhost:{}{}/swagger-ui.html\n\t"
                        + "knife4j: \thttp://localhost:{}{}/doc.html\n"
                        + "----------------------------------------------------------",
                port, contextPath,
                port, contextPath);
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    private static String cleanContextPath(String contextPath) {
        String candidate = StringUtils.trimWhitespace(contextPath);
        return StringUtils.hasText(candidate) && candidate.endsWith("/") ? candidate.substring(0, candidate.length() - 1) : candidate;
    }

}
